{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.019434Z",
     "start_time": "2025-06-24T03:07:27.014693Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from torch.nn import MSELoss\n",
    "from torch.utils import data\n",
    "from d2l import torch as d2l"
   ],
   "outputs": [],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.065647Z",
     "start_time": "2025-06-24T03:07:27.051588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "true_w = torch.tensor([2,-3.4])\n",
    "true_b = 4.2\n",
    "features, labels = d2l.synthetic_data(true_w, true_b,1000)"
   ],
   "id": "cc69a8aa4438c820",
   "outputs": [],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.127497Z",
     "start_time": "2025-06-24T03:07:27.114991Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def load_array(data_arrays, batch_size, is_train=True):\n",
    "\tdataset = data.TensorDataset(*data_arrays)\n",
    "\treturn data.DataLoader(dataset, batch_size, shuffle=is_train)\n",
    "\n",
    "batch_size = 64\n",
    "data_iter = load_array((features,labels), batch_size, True)"
   ],
   "id": "18988f0ab842fe5f",
   "outputs": [],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.174351Z",
     "start_time": "2025-06-24T03:07:27.159290Z"
    }
   },
   "cell_type": "code",
   "source": "next(iter(data_iter))",
   "id": "7a3ec08a12a68136",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[-0.4765, -0.5337],\n",
       "         [ 0.6472, -1.4618],\n",
       "         [ 2.3278, -0.4485],\n",
       "         [ 1.2352,  0.2525],\n",
       "         [ 0.6919, -0.7132],\n",
       "         [-0.1305,  1.5677],\n",
       "         [ 0.8396, -1.4554],\n",
       "         [ 0.5180,  0.8044],\n",
       "         [-0.6769, -1.0635],\n",
       "         [ 0.0921,  0.9056],\n",
       "         [ 0.5289,  0.3767],\n",
       "         [-0.7937, -1.0636],\n",
       "         [ 1.6892,  0.8875],\n",
       "         [-2.3045, -1.1278],\n",
       "         [-0.0706,  0.6211],\n",
       "         [ 2.0494,  0.5421],\n",
       "         [-1.0192,  0.4592],\n",
       "         [ 0.8268,  1.1663],\n",
       "         [ 0.8039, -0.2499],\n",
       "         [-0.4536,  0.4227],\n",
       "         [ 0.8002,  0.5101],\n",
       "         [-0.2467,  0.7668],\n",
       "         [-1.2633,  1.2782],\n",
       "         [-1.6012, -0.5388],\n",
       "         [-0.7964,  1.6258],\n",
       "         [ 1.4297, -1.4167],\n",
       "         [-0.3922,  1.3669],\n",
       "         [ 0.7934,  0.7504],\n",
       "         [-0.4856,  0.3361],\n",
       "         [-0.1778, -2.1576],\n",
       "         [ 0.8945,  1.4050],\n",
       "         [ 2.6492, -0.1034],\n",
       "         [ 0.0922, -0.3956],\n",
       "         [-0.8550,  0.9576],\n",
       "         [ 0.2775, -0.2918],\n",
       "         [ 0.4835,  1.0561],\n",
       "         [ 0.9031,  0.1064],\n",
       "         [-0.0064, -0.2430],\n",
       "         [-0.5062, -1.6998],\n",
       "         [-0.7886, -0.3048],\n",
       "         [ 0.8738, -0.9028],\n",
       "         [-1.0022,  0.2893],\n",
       "         [ 0.3469,  1.2351],\n",
       "         [-0.6093,  0.0325],\n",
       "         [ 0.1665,  0.8976],\n",
       "         [ 2.0858, -0.0378],\n",
       "         [-0.4385, -0.6896],\n",
       "         [ 0.3818,  0.0353],\n",
       "         [ 0.2356, -1.9065],\n",
       "         [ 0.6391, -0.7101],\n",
       "         [ 2.0425, -0.2923],\n",
       "         [-1.1018,  0.7828],\n",
       "         [-0.8184, -0.1546],\n",
       "         [-0.1341, -0.2135],\n",
       "         [-0.4257, -0.5114],\n",
       "         [ 0.2550,  0.3722],\n",
       "         [ 0.8136,  0.7278],\n",
       "         [ 0.4473, -0.1014],\n",
       "         [-1.3248, -0.3826],\n",
       "         [ 0.4116,  0.2594],\n",
       "         [ 0.5206,  0.1797],\n",
       "         [ 0.6171, -0.4222],\n",
       "         [ 1.8248, -0.0537],\n",
       "         [ 1.0974,  0.2150]]),\n",
       " tensor([[ 5.0632],\n",
       "         [10.4490],\n",
       "         [10.3737],\n",
       "         [ 5.8123],\n",
       "         [ 7.9936],\n",
       "         [-1.3874],\n",
       "         [10.8376],\n",
       "         [ 2.5104],\n",
       "         [ 6.4793],\n",
       "         [ 1.3042],\n",
       "         [ 3.9825],\n",
       "         [ 6.2346],\n",
       "         [ 4.5484],\n",
       "         [ 3.4271],\n",
       "         [ 1.9332],\n",
       "         [ 6.4625],\n",
       "         [ 0.5983],\n",
       "         [ 1.9016],\n",
       "         [ 6.6685],\n",
       "         [ 1.8745],\n",
       "         [ 4.0480],\n",
       "         [ 1.1033],\n",
       "         [-2.6866],\n",
       "         [ 2.8397],\n",
       "         [-2.9103],\n",
       "         [11.8784],\n",
       "         [-1.2412],\n",
       "         [ 3.2449],\n",
       "         [ 2.0745],\n",
       "         [11.1836],\n",
       "         [ 1.2020],\n",
       "         [ 9.8707],\n",
       "         [ 5.7155],\n",
       "         [-0.7661],\n",
       "         [ 5.7509],\n",
       "         [ 1.5778],\n",
       "         [ 5.6541],\n",
       "         [ 4.9898],\n",
       "         [ 8.9556],\n",
       "         [ 3.6698],\n",
       "         [ 9.0278],\n",
       "         [ 1.2174],\n",
       "         [ 0.7112],\n",
       "         [ 2.8706],\n",
       "         [ 1.4955],\n",
       "         [ 8.4898],\n",
       "         [ 5.6719],\n",
       "         [ 4.8402],\n",
       "         [11.1585],\n",
       "         [ 7.8815],\n",
       "         [ 9.2860],\n",
       "         [-0.6581],\n",
       "         [ 3.0909],\n",
       "         [ 4.6549],\n",
       "         [ 5.0869],\n",
       "         [ 3.4406],\n",
       "         [ 3.3479],\n",
       "         [ 5.4506],\n",
       "         [ 2.8559],\n",
       "         [ 4.1293],\n",
       "         [ 4.6301],\n",
       "         [ 6.8660],\n",
       "         [ 8.0375],\n",
       "         [ 5.6530]])]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.298700Z",
     "start_time": "2025-06-24T03:07:27.284736Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from torch import nn\n",
    "\n",
    "net = nn.Sequential(nn.Linear(2,1))"
   ],
   "id": "f3c565e6d625fcc8",
   "outputs": [],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.345891Z",
     "start_time": "2025-06-24T03:07:27.331896Z"
    }
   },
   "cell_type": "code",
   "source": "net[0].weight.data.normal_(0.0, 0.01),net[0].bias.data.fill_(0)",
   "id": "e82ed6ee8cca95cb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0.0014, 0.0121]]), tensor([0.]))"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.361294Z",
     "start_time": "2025-06-24T03:07:27.351182Z"
    }
   },
   "cell_type": "code",
   "source": "loss = nn.MSELoss()",
   "id": "525ddda1d8c95961",
   "outputs": [],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.407692Z",
     "start_time": "2025-06-24T03:07:27.393253Z"
    }
   },
   "cell_type": "code",
   "source": "trainer = torch.optim.SGD(net.parameters(), lr=0.03)",
   "id": "b9ebd9748e95bdf8",
   "outputs": [],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.515333Z",
     "start_time": "2025-06-24T03:07:27.439496Z"
    }
   },
   "cell_type": "code",
   "source": [
    "num_epochs = 10\n",
    "for epoch in range(num_epochs):\n",
    "\tfor X, y in data_iter:\n",
    "\t\tl = loss(net(X), y) #计算损失\n",
    "\t\ttrainer.zero_grad() #每一次循环的梯度清零防止累计\n",
    "\t\tl.backward() #反向传播计算梯度\n",
    "\t\ttrainer.step() #训练器根据之前设定的优化算法进行网络参数更新\n",
    "\tl = loss(net(features), labels)\n",
    "\tprint(f'epoch {epoch+1}, loss {l:f}')"
   ],
   "id": "41610ceada2ffeff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 4.819092\n",
      "epoch 2, loss 0.719719\n",
      "epoch 3, loss 0.110465\n",
      "epoch 4, loss 0.016561\n",
      "epoch 5, loss 0.002608\n",
      "epoch 6, loss 0.000481\n",
      "epoch 7, loss 0.000154\n",
      "epoch 8, loss 0.000105\n",
      "epoch 9, loss 0.000097\n",
      "epoch 10, loss 0.000097\n"
     ]
    }
   ],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.561696Z",
     "start_time": "2025-06-24T03:07:27.547664Z"
    }
   },
   "cell_type": "code",
   "source": [
    "w = net[0].weight.data\n",
    "print('w的估计误差：',true_w-w.reshape(true_w.shape))\n",
    "b = net[0].bias.data\n",
    "print('b的估计误差：',true_b-b)"
   ],
   "id": "b6687da32a5f5636",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w的估计误差： tensor([ 3.4380e-04, -3.9101e-05])\n",
      "b的估计误差： tensor([-3.3379e-06])\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-24T03:07:27.607704Z",
     "start_time": "2025-06-24T03:07:27.595338Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "a925ca9f84b464a0",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
